#include "RectangleParser.h"


RectangleParser::RectangleParser(void)
{
}


RectangleParser::~RectangleParser(void)
{
}

RectangleParser::RectangleParser(float x1, float y1, float x2, float y2, string tx_id, string mat_id): PrimitiveParser(tx_id, mat_id)
{
	if(x1<x2){
		this->x1 = x1;
		this->x2 = x2;
	}
	else{
		this->x2 = x1;
		this->x1 = x2;
	}
	if(y1<y2){
		this->y1 = y1;
		this->y2 = y2;
	}
	else{
		this->y2 = y1;
		this->y1 = y2;
	}
}

void RectangleParser::render(){
	if(texture_id != "none" && texture_id != "inherit")
		render(texture);
	else{
		glBegin(GL_POLYGON);
			glNormal3f(0.0,0.0,1.0);
			glVertex3f(x1,y1,0.0);
			glVertex3f(x2,y1,0.0);
			glVertex3f(x2,y2,0.0);
			glVertex3f(x1,y2,0.0);
		glEnd();
	}
}

void RectangleParser::render(TextureParser* tx){
	float txX = (x2 - x1)/ tx->length_s;
	float txY = (y2 - y1)/ tx->length_t;

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, tx->tx_id);
	glBegin(GL_POLYGON);
		glNormal3f(0.0,0.0,1.0);
		glTexCoord2f(0,0); glVertex3f(x1,y1,0.0);
		glTexCoord2f(txX,0); glVertex3f(x2,y1,0.0);
		glTexCoord2f(txX,txY); glVertex3f(x2,y2,0.0);
		glTexCoord2f(0,txY); glVertex3f(x1,y2,0.0);
	glEnd();
	glDisable(GL_TEXTURE_2D);
}